/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.shared;

import edu.idp.shared.utils.Convert;

import java.awt.*;

/**
 * @author Kristopher T Babic
 */
public class StoredObject extends Object {

    public static final int FREEHAND = 0;
    public static final int LINE = 1;
    public static final int ELLIPSE = 2;
    public static final int RECTANGLE = 3;
    public static final int ANIMATED = 4;
    public static final int AIRBRUSH = 5;

    public static final int MIN_TYPE = 0;
    public static final int MAX_TYPE = 5;

    public static final long DEFAULT_DEATH = 326592000000l;//32659200000l;

    private long storeDate = 0;
    private long storeDeath = DEFAULT_DEATH;
    private int storeType = -1;
    private Color storeColor = null;
    private String storeExtra = null;
    private Rectangle storeBounds = null;
    private String storeID = "";

    public StoredObject() {
    }

    public StoredObject(Object obj) {
        if (!isValid(obj))
            return;
        //  should throw EXCEPTION????

        String object = (String) obj;

        setType(findType(object));
        setDate(findDate(object));
        setDeath(findDeath(object));
        setColor(findColor(object));
        setBounds(findBounds(object));
        setID(findID(object));
        setExtra(findExtra(object));

    }

    public long getDate() {
        return storeDate;
    }

    public long getDeath() {
        return storeDeath;
    }

    public Color getColor() {
        return storeColor;
    }

    public int getType() {
        return storeType;
    }

    public String getExtra() {
        return storeExtra;
    }

    public Rectangle getBounds() {
        return storeBounds;
    }

    public String getID() {
        return storeID;
    }

    public void setDate(long date) {
        this.storeDate = date;
    }

    public void setDeath(long death) {
        this.storeDeath = death;
    }

    public void setType(int type) {
        this.storeType = type;
    }

    public void setExtra(String extra) {
        this.storeExtra = (extra == null) ? "" : extra;
    }

    public void setBounds(Rectangle bounds) {
        this.storeBounds = bounds;
    }

    public void setColor(Color color) {
        this.storeColor = color;
    }

    public void setID(String id) {
        if (id.length() < 2 + edu.idp.shared.net.NetTransferObject.MAX_LEN) {
            for (int i = 2 + edu.idp.shared.net.NetTransferObject.MAX_LEN; i > 0; i--)
                id += (char) 1;
        }
        this.storeID = id;
    }

    public String toString() {
        return Convert.fromInt(getType())
                + Convert.fromColor(getColor())
                + Convert.fromLong(getDate())
                + Convert.fromLong(getDeath())
                + Convert.fromRectangle(getBounds())
                + getID()
                + getExtra();
    }

    public static int findType(String in) {
        return Convert.toInt(in.substring(0, 2));
    }

    public static Color findColor(String in) {
        return Convert.toColor(in.substring(2, 4));
    }

    public static long findDate(String in) {
        return Convert.toLong(in.substring(4, 8));
    }

    public static long findDeath(String in) {
        return Convert.toLong(in.substring(8, 12));
    }

    public static Rectangle findBounds(String in) {
        return Convert.toRectangle(in.substring(12, 20));
    }

    public static String findID(String in) {
        return in.substring(20, 22 + edu.idp.shared.net.NetTransferObject.MAX_LEN);
    }

    public static String findExtra(String in) {
        return in.substring(22 + edu.idp.shared.net.NetTransferObject.MAX_LEN, in.length());
    }

    public static boolean isValid(Object o) {
        if (!(o instanceof String))
            return false;


        String object = (String) o;
        if (object == null || object.length() < 22 + edu.idp.shared.net.NetTransferObject.MAX_LEN)
            return false;

        int type = findType(object);

        return (type >= MIN_TYPE && type <= MAX_TYPE);
    }

    public static boolean isType(String object, int type) {
        if (object == null || object.length() < 3)
            return false;
        return (type == findType(object));
    }

} 